package com.one.efaimaly.order.ui;

import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.common.order.AddMaterialItem;
import com.one.common.common.order.MaterialStateItem;
import com.one.common.config.RouterPath;
import com.one.common.view.base.IListView;
import com.one.common.view.pagestate.listpage.BaseListActivity;
import com.one.common.view.widget.MyTitleBar;
import com.one.efaimaly.R;
import com.one.efaimaly.order.presenter.MaterialPresenter;
import com.one.common.common.order.binder.MaterialInfoBinder;
import com.one.common.common.order.binder.MaterialStateBinder;

/**
 * creator: zhulunjun
 * time:    2018/8/10
 * describe: 确认选择的材料
 */
@Route(path = RouterPath.CONFIRM_MATERIAL)
public class ConfirmMaterialActivity extends BaseListActivity<MaterialPresenter> implements IListView {

    @Override
    protected void initPresenter() {
        mPresenter = new MaterialPresenter(this, this);
    }

    @Override
    public void registerDate() {
        register(MaterialStateItem.class, new MaterialStateBinder());
        register(AddMaterialItem.class, new MaterialInfoBinder());
    }

    @Override
    public void loadData() {
        if (mPresenter.getListExtra() != null) {

            if (mPresenter.getPayItems().size() > 0) {
                addData(new MaterialStateItem(false));
                for (AddMaterialItem item : mPresenter.getPayItems()) {
                    addData(item);
                }
            }
            if (mPresenter.getFreeItems().size() > 0) {
                addData(new MaterialStateItem(true));
                for (AddMaterialItem item : mPresenter.getFreeItems()) {
                    addData(item);
                }
            }
        }
        if(mPresenter.getTotalPrice()>0) {
            addData(new AddMaterialItem(mPresenter.getTotalPrice()));
        }
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("确认订单");
    }

    @Override
    public void initView() {
        super.initView();
        hindLoadMore();
        hindRefresh();
        addTopView(R.layout.layout_confirm_material_top);
        addBottomView(R.layout.layout_confirm_material_bottom);

        successView.findViewById(R.id.tv_btn_update).setOnClickListener(this);
        successView.findViewById(R.id.tv_btn_add).setOnClickListener(this);
        successView.findViewById(R.id.tv_btn_confirm).setOnClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        loadData();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_btn_update: // 点击修改，带参数不显示添加按钮
                mPresenter.updateToMaterials(true);
                break;
            case R.id.tv_btn_add:
                mPresenter.updateToMaterials(false);
                break;
            case R.id.tv_btn_confirm:
                mPresenter.addMaterials();
                break;
            default:
                break;
        }
    }
}
